package helper

import (
	"github.com/gin-gonic/gin"
	"github.com/slclub/go-tips"
)

func Uid(ctx *gin.Context) string {
	id_s := ctx.GetHeader("DeviceID")

	if id_s != "" {
		return id_s
	}

	ip := ctx.RemoteIP()
	if ip == "" || ip == "::1" {
		ip = "127.0.0.1"
	}

	return tips.String(UidWithIP(ip))
}

func UidWithIP(ip string) uint {
	uid, err := Ipv4ToLong(ip)
	if err != nil {
		return 0
	}
	return uid
}
